Macromedia
Instructor-Led Training
Connecting to a Database With ColdFusion MX
Dynamic content on the web is no longer a luxury: It is a necessity. With ColdFusion MX, generating dynamic content by accessing a database is easy, and can help you offer your customers higher quality content, while at the same time making site maintenance easier. This article introduces you database connectivity with ColdFusion MX and Dreamweaver MX. In it, you will learn how to:
- Connect to a database.
- Query the database.
- Display the query results on a web page.
This article covers just a small portion of the Fast Track to ColdFusion MX instructor-led course offered by Macromedia Authorized Training partners. If you like what you see and want to get the full experience, find a Macromedia Authorized Training Partner near you. Only at a Macromedia Authorized Training location will you learn from instructors thoroughly trained and certified by Macromedia. You can learn more in a few days with a qualified trainer than you can learn in a year with a book. Get the skills you need to get ahead - Macromedia Training and Certification.
System Requirements
To access a database with ColdFusion MX, you need:
- ColdFusion MX
- Dreamweaver MX
- A web browser
-
Download the Microsoft Access database sample file for this article
(36 KB ZIP file).
Note: While these requirements work for a development environment, they are not adequate for hosting a production-level site.
Setting up Dreamweaver MX to Create ColdFusion Applications
Setting up Dreamweaver MX to create ColdFusion pages and applications is very similar to setting up Dreamweaver MX for creating HTML pages.
- Open Dreamweaver MX
- Select Site New Site...from the Menu bar to create a new site
- On the Basic tab, enter the following information,
clicking Next at the bottom of each screen:
- Name: MyColdFusion.
- Yes, I want to use a server technology. Select ColdFusion as the server technology.
- Edit and test locally, storing your files at C:\CFusionMX\wwwroot\MyColdFusion\.
- URL to browse to the root of your site http://localhost:8500/MyColdFusion/.
Note: If you are using IIS or Apache web server instead of the built-in JRun Web Server (JWS) for ColdFusion MX, do not use port 8500 in the URL. Also, the physical directory you specified in previous step, "Edit and test locally, storing your files at", must correspond to the webroot for your web server. Check the your web server's documentation to figure out the physical directory for your webroot.
- No, do not copy files when I am done editing
- Click Done to create an initial site cache.
- If you have not already done so,
download the Microsoft Access database sample file for this article
(36 KB ZIP file). - Unzip the MySource.zip file and move the MySource.mdb database file into the C:\CFusionMX\wwwroot\MyColdFusion\ directory you created in step 3 above.
Note: You should never save any database within the web root, or any of it's subdirectories in a production web application. Doing this is a security risk, as it enables users to download the database if they can guess the name and directory location of the database.
About Dynamic Content
In the early days of the World Wide Web, most websites were just repositories of static information. But as the web has grown in capacity, it has also grown in capability. Today, organizations want to do more with their websites, such as provide customized information for users and allow people to conduct commercial transactions. These capabilities require dynamic web pages.
Static web pages
Simple web applications deliver static pages. As the term implies, content on static pages does not change. A developer creates a static page and puts it on a server where a user can request it. The components of a static page, such as text, images, and other embedded objects, are present in the code of the page, and change only when a developer manually modifies the page. Static pages are processed as follows:
- A user requests the page by typing its URL in a browser.
- The browser requests the page from the web server.
- The web server locates the page and sends it to the browser in the form of HTML.
- The browser interprets the HTML and displays the page to the user.
Dynamic web pages
In many cases, a web page must be able to deliver content that changes, either over time or by a user. For example, the following scenarios require dynamic processing:
- A user wants the latest stock quotes and needs up-to-the-minute information.
- A user purchasing items from a website needs information that is specific to her.
- A customer browsing a catalog needs updated information stored within a database.
Dynamic page processing is similar to static page processing, but includes some additional steps:
- A user requests the page by typing its URL in a browser.
- The browser requests the page from the web server.
- The web server processes the unique page extension and passes the request on to an application server for processing.
- The application server processes the request and outputs some data in the form of HTML back to the web server. (For example, the dynamic web page may instruct the application server to extract data from a database and insert it into the page's HTML.)
- The web server sends the resulting HTML page to the browser.
- The browser interprets the HTML and displays the page to the user.
About ColdFusion MX
ColdFusion MX is an application server. An application server is software that runs on a server, works with the web server, and extends the capabilities of the web server. When the web server detects a page's CFM extension, it passes the page to ColdFusion MX for processing.
At its most basic, ColdFusion development consists of one server that hosts the ColdFusion MX server, the web server, and the database. In the best of scenarios, however, the database resides on a separate server computer. Dreamweaver MX is the primary editor for developing ColdFusion application pages.
This basic configuration may work for very small applications with one developer, but a true development system would include separate systems for development, testing, and production.
ColdFusion MX can read a number of programming languages, including the ColdFusion Markup Language (CFML). The ColdFusion Markup Language (CFML) is a tag-based language that the ColdFusion server processes This language is similar to HTML; however, all ColdFusion tags begin with the letters "CF". The CFML tags signal the ColdFusion MX application server to perform specific operations. Each tag identifies an operation, such as including a block of code from another file or setting a variable. You can combine CFML tags and HTML tags on one dynamic web page.
Because Macromedia designed Dreamweaver MX to support ColdFusion MX, you can establish a database connection without manually coding any ColdFusion tags yourself—Dreamweaver MX does that for you.
Exercise: Setting Up a Database Connection in ColdFusion MX
- In Dreamweaver, open the MyColdFusion site you defined at the beginning of this article.
- Within Dreamweaver, select File New.
- In the General tab of the New Document dialog box, select Dynamic Page in the Category list, and then select ColdFusion in the Dynamic Page list.
- Click Create.
-
On the Applications panel, select the Databases tab, and then click the Modify Data Sources button.

- Enter MyDataSource in the Data Source Name text box.
- Select Microsoft Access in the Driver pop-up menu.
- Click Add.
- Click Browse Server next to the Database File
field, select MySource.mdb from C:\CFusionMX\wwwroot\MyColdFusion\,
and click Apply.
Note: Make sure you check the checkbox for Default Username.
- Click Submit to add the new data source to the list. In the ColdFusion MX Administrator, look for MyDataSource in the list of connected data sources with a status of OK.
- Close the ColdFusion MX Administrator.
- Return to Dreamweaver MX.
In the Applications panel, select the Databases tab, and then click the Refresh button.
- Ensure MyDataSource appears in the data source list.
- Save the page as MyColdFusionPage.cfm in C:\CFusionMX\wwwroot\MyColdFusion\.
Talking to the Database
After you have established a connection to the database, you need to be able to talk to the database. Most databases use Structured Query Language (SQL) to talk to the outside world. SQL uses statements to access and manipulate data in a database. Dreamweaver MX allows you to build SQL statements, or queries, without having to write any SQL at all. Database queries return recordsets, which contain content from the database. Using the Bindings panel, you can create queries without manually writing SQL statements. By default, this panel belongs to the Application panel group.
To create a query, press the plus (+) button at the upper left of the panel and select Recordset (Query). This opens up the Recordset dialog box as shown in the illustration below.
When you define a recordset through this dialog box, Dreamweaver MX writes a SQL statement that queries the database, and that query returns a recordset. Once you create a query, you will find it in the Bindings panel. Not only does the recordset appear in the Bindings panel, but Dreamweaver MX also adds the appropriate code to your page, so that you can use the recordset on another page.
Exercise: Querying a Database
- Open MyColdFusionPage.cfm from C:\CFusionMX\wwwroot\MyColdFusion\.
- Open the Application panel and click the Bindings panel.
Click the plus sign and select Recordset (Query).
- Be sure you are in simple mode. If you see a button labeled Advanced between the Test and Help buttons, you are in simple mode.
- Enter qGetDistList in the Name field.
- Select MyDataSource in the Data Source pop-up menu.
- Select Distributor in the Table pop-up menu.
- Click the Selected Columns radio button.
- Hold down the Control key on the keyboard and select the column Distributor_ID and Distributor_Name.
- Select Distributor_Name in the Sort pop-up menu and Ascending order.
- Click Test to test the recordset. You should see the query results return nine records. Click OK to close the Test SQL Statement dialog box.
- Click OK to close the Recordset dialog box and bind the recordset to MyColdFusionPage.cfm.
- From the Document toolbar, select Show
Code and Design Views. Scroll to the top of the page. Notice that Dreamweaver adds the
cfquerytag to the top of the page. In addition, the recordset is available on the Bindings panel in the Application panel.Note: To edit the query, double-click the recordset on the Bindings panel to open the Recordset dialog box.
- Return to Design view.
- Save the file.
Displaying a Recordset on the Page
After you define a query for a page, you can insert the dynamic content that the recordset returns from the database into the page. Dreamweaver MX provides more than one way to display dynamic content on a page. Here you will read how to display database data using the Dynamic Table tool.
Find the Dynamic Table tool in the Application tab of the Insert bar.
Click the Dynamic Table button to open the Dynamic Table dialog box:
You can select the recordset and the number of records to display per page, as well as the HTML table border, cell padding, and cell spacing attributes used to format the table. After you click OK, an HTML table containing the recordset appears on your page with the appropriate code.
If you view the page in Design view, only one row shows, even though you specified multiple records for display, and that row contains dynamic text placeholders rather than actual data. But if you browse the page, it displays one row for each record in the database.
Displaying Database Content
- Open MyColdFusionPage.cfm from C:\CFusionMX\wwwroot\MyColdFusion\.
- Position your cursor in the Document window.
- In the Insert bar, click the Application tab, and click Dynamic Table.
- Enter the following information in the Dynamic
Table dialog box that opens, and then click OK:
- Recordset:
qGetDistList - Show: All Records
- Border: 1
- Cell Padding: 2
- Cell Spacing: 2
- Recordset:
- Select the first table row by dragging the mouse across the two columns. From the Properties inspector, select the Header check box.
- Modify the labels so they do not include the _ (underscore) character.
- Your table should look like this:
- Save the file.
- From the Document toolbar, select Preview/Debug in Browser Preview in iexplore.
- Close the browser window and return to Dreamweaver MX.
- Position your cursor in the Document window above the table.
- In the Insert bar, click the Text tab and add an <h2> tag.
- Add the following text for the page header: Daily Bean Specials.
- Position the cursor in front of the word "Daily".
- In the Application panel, click the Server Behaviors panel. Click the plus sign and select Display Record Count Display Total Records. In the Display Total Records dialog box, select qGetDistList and click OK.
- Save the file and browse it.
- Close the browser window and return to Dreamweaver MX.
Conclusion
ColdFusion MX makes creating dynamic content quick and
easy. And Dreamweaver MX is a great authoring environment for developing
ColdFusion pages. As you have seen, it is very easy to connect to a
database, query the database, and display the results of that query
on a web page using ColdFusion and Dreamweaver MX. This article has
touched upon only a small portion of what you can do with Dreamweaver
MX and ColdFusion. To see more, take the Fast Track to ColdFusion MX
instructor-led course at a Macromedia Authorized Training Partner near
you.
Submit feedback on our tutorials, articles, and sample applications.